package com.yanqu.road.server.cmd.union.cross.trade;

import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.trade.UnionTradeRouteInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.UnionTradeProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.union.UnionTradeModule;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.server.manger.union.UnionTradeMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_UNION_TRADE_ROUTE_OPEN, desc = "开启路线")
public class TradeRouteOpenCmd extends AbstractCommand {

    private int routeId;

    private int goodId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionTradeProto.RouteOpenReqMsg routeOpenReqMsg = UnionTradeProto.RouteOpenReqMsg.parseFrom(packet.getBytes());
        routeId = routeOpenReqMsg.getRouteId();
        goodId = routeOpenReqMsg.getGoodId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        UnionTradeRouteInfo routeInfo = UnionConfigMgr.getUnionTradeRouteInfo(routeId);
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        if(null == unionInfo){
            ret = GameErrorCode.E_UNION_TRADE_ROUTE_NOT_FOUND;
            return;
        }
        if(!UnionTradeMgr.isRouteUnlock(routeInfo, unionInfo)){
            ret = GameErrorCode.E_UNION_TRADE_ROUTE_LOCKED;         //未解锁
            return;
        }

        //开启条件
        UnionMember unionMember = CrossUnionMgr.getUnionMember(unionUid, player.getUserId());

        Property property = new Property();
        Property consume = PropertyHelper.parseStringToProperty(routeInfo.getConsume(), "\\|");
        if(consume.getCountByGoodsId(goodId).compareTo(BigInteger.ZERO) == 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;                        //参数类型不对
            return;
        }
        //是否足够消耗
        if (GameConfig.GAME_ITEM_UNION_WEALTH == goodId) {
            if (unionMember.getPosition() > eUnionPosition.DeputyMaster.getValue()) {
                ret = GameErrorCode.E_UNION_TRADE_ONLY_MASTER_CAN_USE_WEALTH;    //只有商会正、副会长可用商会财富
                return;
            }
            property.setGood(GameConfig.GAME_ITEM_UNION_WEALTH,  consume.getCountByGoodsId(GameConfig.GAME_ITEM_UNION_WEALTH));
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) {
                ret = GameErrorCode.E_UNION_TRADE_WEALTH_NO_ENOUGH;              //商会财富不足
                return;
            }
        }else {
            property.setGood(goodId, consume.getCountByGoodsId(goodId));
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;                       //道具不足
                return;
            }
        }

        UnionTradeProto.RouteOpenCrossReqMsg.Builder crossReq = UnionTradeProto.RouteOpenCrossReqMsg.newBuilder();
        crossReq.setRouteId(routeId);
        crossReq.setConsume(PropertyHelper.parsePropertyToString(property));
        player.sendPacket(Protocol.C_UNION_TRADE_ROUTE_OPEN, crossReq);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionTradeProto.RouteOpenRespMsg.Builder routeOpenResp = UnionTradeProto.RouteOpenRespMsg.newBuilder();
            routeOpenResp.setRet(ret);
            player.sendPacket(Protocol.U_UNION_TRADE_ROUTE_OPEN, routeOpenResp);
        }
    }
}
